package edu.gatech.cc.hwa.server.database;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.junit.After;
import org.junit.AfterClass;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;

import edu.gatech.cc.hwa.server.User;
import edu.gatech.cc.hwa.server.database.Database;
import edu.gatech.cc.hwa.server.database.exceptions.DatabaseException;
import edu.gatech.cc.hwa.server.database.exceptions.UserAlreadyExistsException;

public class DatabaseTests {

	private static final String URL = "jdbc:mysql://localhost";
	private static final String USER = "root";
	private static final String PWD = "admin";
	
	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
		
	}
	
	@AfterClass
	public static void tearDownAfterClass() {
		try {
			Connection con = DriverManager.getConnection(URL, USER, PWD);
			con.createStatement().execute("DELETE FROM test.User");
			con.createStatement().execute("DELETE FROM test.Profile");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	@Test
	public void testCreateUser() throws DatabaseException {
		String username = "user_testCreateUser";
		Database.createUser(username);
		User user = Database.getUser(username);
		assertNotNull(user);
	}
	
	@Test(expected=UserAlreadyExistsException.class)
	public void testCreateDuplicateUser() throws DatabaseException {
		String username = "user_testCreateDuplicateUser";
		Database.createUser(username);
		Database.createUser(username);
	}

}
